<template>
  <div>
    <!--面包屑导航区域-->
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>权限管理</el-breadcrumb-item>
      <el-breadcrumb-item>角色列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!--卡片视图-->
    <el-card>
      <!--添加角色按钮区域-->
      <el-row>
        <el-button type="primary">添加角色</el-button>
      </el-row>

      <el-table :data="rolelist" border stripe>
        <el-table-column type="expand">
          <template slot-scope="scope">
            <el-row :class="['bdbottom',i1===0?'bdtop':'','vcenter']" v-for="(item1,i1) in scope.row.children"
              :key="item1.id">
              <!--渲染一级权限-->
              <el-col :span="5">
                <el-tag closable @close="removeRightById(scope.row,item1.id)">{{ item1.authName }}</el-tag>
                <i class="el-icon-caret-right"></i>
              </el-col>
              <!--渲染二级和三级权限-->
              <el-col :span="19">
                <el-row :class="[i2===0?'':'bdtop','vcenter']" v-for="(item2,i2) in item1.children" :key="item2.id">
                  <!--通过for 循环 嵌套渲染二级权限-->
                  <el-col :span="6">
                    <el-tag type="success" closable @close="removeRightById(scope.row,item2.id)">{{ item2.authName }}
                    </el-tag><i class="el-icon-caret-right"></i>
                  </el-col>
                  <el-col :span="18">
                    <el-tag v-for="(item3,i3) in item2.children" :key="item3.id" type="warning" closable
                      @close="removeRightById(scope.row,item3.id)">{{ item3.authName }}</el-tag>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>

          </template>
        </el-table-column>
        <el-table-column type="index" label="#">
        </el-table-column>
        <el-table-column label="角色名称" prop="roleName">
        </el-table-column>
        <el-table-column label="角色描述" prop="roleDesc">
        </el-table-column>
        <el-table-column label="操作" width="300px">
          <template slot-scope="scope">
            <el-button type="primary" size="mini" icon="el-icon-edit">编辑</el-button>
            <el-button type="danger" size="mini" icon="el-icon-delete">删除</el-button>
            <el-button type="warning" size="mini" icon="el-icon-setting" @click="showSetRightDialog(scope.row)">分配权限
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <!--分配权限的对话框-->

    <el-dialog title="分配权限" :visible.sync="setRightDialogVisible" width="50%" @close="setRightDialogClosed">
      <!--树形控件-->
      <el-tree :data="rightslist" :props="treeProps" show-checkbox @node-click="handleNodeClick" node-key="id"
        default-expand-all :default-checked-keys="defKeys" ref="treeRef"></el-tree>

      <span slot="footer" close="dialog-footer">
        <el-button @click="setRightDialogVisible=false">取消</el-button>
        <el-button type="primary" @click="allotRights">确定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
  export default {
    data() {
      return {
        //所有角色列表
        rolelist: [],
        //控制对话框的展示与隐藏
        setRightDialogVisible: false,
        //所有权限为空
        rightslist: [],
        //默认选择节点的数据
        defKeys: [],
        //当前即将分配角色的ID
        roleId:'',
        treeProps: {
          children: 'children',
          label: 'authName'
        }
      }
    },
    created() {
      this.getRolesList()
    },
    methods: {
      async getRolesList() {

        // const {
        //   res: data
        // } = await this.$http.get('roles')
        // if (res.meta.status != 200) {
        //   return this.$message.error('获取角色列表失败')
        // }
        this.rolelist = [{
            id: '1',
            roleName: '主管',
            roleDesc: '技术负责人',

            children: [{
                id: '01',
                authName: '商品管理',
                path: '',
                children: [{
                    id: '0101',
                    authName: '商品列表',
                    path: '',
                    children: [{
                      id: '010102',
                      authName: '商品修改',
                      path: ''
                    }]
                  },
                  {
                    id: '0102',
                    authName: '分类参数',
                    path: '',
                    children: [{
                        id: '010201',
                        authName: '获取参数列表',
                        path: ''
                      },
                      {
                        id: '010202',
                        authName: '创建商品参数',
                        path: ''
                      },
                      {
                        id: '10203',
                        authName: '删除商品参数',
                        path: ''
                      }
                    ]
                  },
                  {
                    id: '103',
                    authName: '商品分类',
                    path: '',
                    children: [{
                        id: '10301',
                        authName: '添加分类',
                        path: ''
                      },
                      {
                        id: '10302',
                        authName: '删除分类',
                        path: ''
                      },
                      {
                        id: '10303',
                        authName: '获取分类详情',
                        path: ''
                      }
                    ]
                  }
                ]
              },
              {
                id: '11',
                authName: '订单管理',
                path: '',
                children: [{
                    id: '111',
                    authName: '订单列表',
                    path: '',
                    children: [{
                      id: '1111',
                      authName: '添加订单',
                      path: ''
                    }]
                  }

                ]
              },
              {
                id: '12',
                authName: '权限管理',
                path: '',
                children: [{
                    id: '121',
                    authName: '角色列表',
                    path: '',
                    children: [{
                        id: '1211',
                        authName: '添加角色',
                        path: ''
                      },
                      {
                        id: '1212',
                        authName: '删除角色',
                        path: ''
                      },
                      {
                        id: '1213',
                        authName: '角色授权',
                        path: ''
                      },
                      {
                        id: '1214',
                        authName: '取消角色授权',
                        path: ''
                      },
                      {
                        id: '1215',
                        authName: '获取角色列表',
                        path: ''
                      },
                      {
                        id: '1216',
                        authName: '获取角色详情',
                        path: ''
                      },
                      {
                        id: '1217',
                        authName: '更新角色信息',
                        path: ''
                      },
                      {
                        id: '1218',
                        authName: '更新角色权限',
                        path: ''
                      }
                    ]
                  },
                  {
                    id: '122',
                    authName: '权限列表',
                    path: ''
                  }
                ]
              },
              {
                id: '13',
                authName: '用户管理',
                path: '',
                children: [{
                  id: '131',
                  authName: '用户列表',
                  path: '',
                  children: [{
                      id: '1311',
                      authName: '添加用户',
                      path: ''
                    },
                    {
                      id: '1312',
                      authName: '删除用户',
                      path: ''
                    },
                    {
                      id: '1313',
                      authName: '更新用户',
                      path: ''
                    },
                    {
                      id: '1314',
                      authName: '获取用户详情',
                      path: ''
                    },
                    {
                      id: '1315',
                      authName: '分配用户角色',
                      path: ''
                    }
                  ]
                }]
              },
              {
                id: '14',
                authName: '数据统计',
                path: '',
                children: [{
                  id: '141',
                  authName: '数据报表',
                  path: ''
                }]
              }
            ]
          },
          {
            id: '2',
            roleName: '测试用户',
            roleDesc: '测试用户描述',

            children: [{
              id: '21',
              authName: '',
              path: '',
              children: [{
                id: '211',
                authName: '',
                path: ''
              }]
            }]
          },
          {
            id: '3',
            roleName: '测试用户2',
            roleDesc: '测试描述12',

            children: [{
              id: '31',
              authName: '',
              path: '',
              children: [{
                id: '311',
                authName: '',
                path: ''
              }]
            }]
          },
          {
            id: '4',
            roleName: '大发送到',
            roleDesc: '大发送到12',
            children: [{
              id: '41',
              authName: '',
              path: '',
              children: [{
                id: '411',
                authName: '',
                path: ''
              }]
            }]
          }
        ]

      },
      async removeRightById(role, rightId) {
        const confirmResult = await this.$confirm('此操作将永久删除该用户，是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).catch(err => err)
        console.log(confirmResult)

        if (confirmResult !== 'confirm') {
          return this.$message.info('已取消删除')
        }
        this.$message.success('删除成功！！！')

        // const {
        //   res: data
        // } = await this.$http.delete(`roles/${role.id}/rights/${rightId}`)

        // if (res.meta.status !== 200) {
        //   return this.$message.info('删除用户失败！')
        // }
        // role.children=res.data()
        // this.getRolesList()
      },
      //展示分配权限
      async showSetRightDialog(role) {
        this.roleId=role.id
        
        //获取所有权限的数据
        // const { data:res }=await this.$http.get('rights/tree')

        //把获取到的所有权限数据放入data中
        this.rightslist = [{
            id: '01',
            authName: '商品管理',
            children: [{
                id: '0101',
                authName: '商品列表',
                children: [{
                    id: '010101',
                    authName: '添加商品'
                  },
                  {
                    id: '010102',
                    authName: '商品修改'
                  },
                  {
                    id: '010103',
                    authName: '商品删除'
                  },
                  {
                    id: '010104',
                    authName: '更新商品图片'
                  },
                  {
                    id: '010105',
                    authName: '更新商品属性'
                  },
                  {
                    id: '010106',
                    authName: '更新商品状态'
                  },
                  {
                    id: '010107',
                    authName: '获取商品详情'
                  }
                ]
              },
              {
                id: '0102',
                authName: '分类参数',
                children: [{
                    id: '010201',
                    authName: '获取参数列表'
                  },
                  {
                    id: '010202',
                    authName: '创建商品参数'
                  },
                  {
                    id: '010203',
                    authName: '删除商品参数'
                  }
                ]
              },
              {
                id: '0103',
                authName: '商品分类',
                children: [{
                    id: '010301',
                    authName: '添加分类'
                  },
                  {
                    id: '010302',
                    authName: '删除分类'
                  },
                  {
                    id: '010303',
                    authName: '获取分类详情'
                  }
                ]
              }
            ]
          },
          {
            id: '02',
            authName: '订单管理',
            children: [{
              id: '0201',
              authName: '订单列表',
              children: [{
                  id: '020101',
                  authName: '添加订单'
                },
                {
                  id: '020102',
                  authName: '订单更新'
                },
                {
                  id: '020103',
                  authName: '获取订单详情'
                }
              ]
            }]
          },
          {
            id: '03',
            authName: '权限管理',
            children: [{
                id: '0301',
                authName: '角色列表',
                children: [{
                    id: '030101',
                    authName: '添加角色'
                  },
                  {
                    id: '030102',
                    authName: '删除角色'
                  },
                  {
                    id: '030103',
                    authName: '角色授权'
                  },
                  {
                    id: '030104',
                    authName: '取消角色授权'
                  },
                  {
                    id: '030105',
                    authName: '获取角色列表'
                  },
                  {
                    id: '030106',
                    authName: '更新角色信息'
                  }
                ]
              },
              {
                id: '0302',
                authName: '权限列表',
                children: [{
                  id: '030201',
                  authName: '三级 3-2-1'
                }]
              }
            ]
          },
          {
            id: '04',
            authName: '用户管理',
            children: [{
                id: '0401',
                authName: '二级 3-1',
                children: [{
                  id: '040101',
                  authName: '三级 3-1-1'
                }]
              },
              {
                id: '0402',
                authName: '二级 3-2',
                children: [{
                  id: '040201',
                  authName: '三级 3-2-1'
                }]
              }
            ]
          },
          {
            id: '05',
            authName: '数据统计',
            children: [{
                id: '0501',
                authName: '二级 3-1',
                children: [{
                  id: '050101',
                  authName: '三级 3-1-1'
                }]
              },
              {
                id: '0502',
                authName: '二级 3-2',
                children: [{
                  id: '050201',
                  authName: '三级 3-2-1'
                }]
              }
            ]
          }
        ]
        // this.defKeys=['010101','010102']
        // this.defKeys=['0101']
        console.log(role)
        //递归获取三级节点的ID
        this.getLeafKeys(role, this.defKeys)
        this.setRightDialogVisible = true
      },
      //通过递归的形式，获取角色下所有三级权限的ID，并保存到数组中
      getLeafKeys(node, arr) {
        if (!node.children) {
          //如果当前节点不包含children属性，则是三级节点
          return arr.push(node.id)
        }
        node.children.forEach(item => this.getLeafKeys(item, arr))
      },
      handleNodeClick(data) {
        console.log(data);
      },
      //监听分配权限对话框的关闭事件
      setRightDialogClosed(){
        this.defKeys=[]
      },
     async allotRights(){
        const keys=[ this.$refs.treeRef.getHalfCheckedKeys(), this.$refs.treeRef.getCheckedKeys() ]
        const idStr=keys.join(',')
        this.getRolesList()
        this.setRightDialogVisible=false
        // const { data:res } =await this.$http.post(`roles/${this.roleId}/rights`,{ rids:idStr })
        
      }
      
    }
  }
</script>

<style>
  .bdtop {
    border-top: 1px solid #eee;
  }

  .bdbottom {
    border-bottom: 1px solid #eee;
  }

  .el-tag {
    margin: 7px;
  }

  .vcenter {
    display: flex;
    align-items: center;
  }
</style>
